<template>
  <div class="mobile-login-form">
    h5
    <el-form ref="formRef" :model="loginForm" :rules="loginRules" size="large">
      <!-- 手机号登录 -->
      <el-form-item prop="phone">
        <el-input
          v-model="loginForm.phone"
          placeholder="请输入手机号"
          :prefix-icon="Iphone"
          maxlength="11"
          clearable
          @keyup.enter="handleLogin"
        />
      </el-form-item>

      <!-- 验证码输入 -->
      <!-- <el-form-item prop="smsCode">
        <div class="sms-code-container">
          <el-input
            v-model="loginForm.smsCode"
            placeholder="请输入验证码"
            maxlength="6"
            :prefix-icon="Key"
            style="flex: 1"
            @keyup.enter="handleLogin"
          />
          <el-button
            :disabled="!!timer || loading"
            type="primary"
            @click="handleSendSmsCode"
          >
            {{ smsButtonText }}
          </el-button>
        </div>
      </el-form-item> -->

      <el-form-item>
        <el-button :loading="loading" type="primary" style="width: 100%" @click="handleLogin">
          登录
        </el-button>
      </el-form-item>

      <!-- 快捷登录选项 -->
      <!-- <div class="quick-login">
        <el-divider>快捷登录</el-divider>
        <div class="quick-login-icons">
          <div class="quick-login-item" @click="handleQuickLogin('wechat')">
            <icon-wechat class="icon" />
            <span>微信</span>
          </div>
          <div class="quick-login-item" @click="handleQuickLogin('alipay')">
            <icon-alipay class="icon" />
            <span>支付宝</span>
          </div>
        </div>
      </div> -->
    </el-form>

    <!-- 用户协议 -->
    <!-- <div class="agreement">
      <el-checkbox v-model="loginForm.agreement" @change="handleAgreementChange">
        我已阅读并同意
      </el-checkbox>
      <el-link type="primary" @click="showAgreement">《用户协议》</el-link>
      <span>和</span>
      <el-link type="primary" @click="showPrivacy">《隐私政策》</el-link>
    </div> -->
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Iphone, Key } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/modules/user'
import { useRoute, useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'

const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const loading = ref(false)
const timer = ref<number | null>(null)
const countdown = ref(60)
const smsButtonText = ref('获取验证码')

const loginForm = reactive({
  phone: '',
  smsCode: '',
  agreement: false
})

const loginRules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  smsCode: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { pattern: /^\d{6}$/, message: '验证码为6位数字', trigger: 'blur' }
  ]
}

const formRef = ref<FormInstance>()

const handleSendSmsCode = async () => {
  if (!loginForm.phone) {
    ElMessage.warning('请先输入手机号')
    return
  }
  
  if (!loginForm.agreement) {
    ElMessage.warning('请先同意用户协议和隐私政策')
    return
  }

  try {
    // 发送验证码的API调用
    await userStore.sendSmsCode(loginForm.phone)
    ElMessage.success('验证码发送成功')
    
    // 开始倒计时
    countdown.value = 60
    smsButtonText.value = `${countdown.value}s后重新获取`
    timer.value = window.setInterval(() => {
      countdown.value--
      smsButtonText.value = `${countdown.value}s后重新获取`
      if (countdown.value <= 0) {
        clearInterval(timer.value!)
        timer.value = null
        smsButtonText.value = '获取验证码'
      }
    }, 1000)
  } catch (error) {
    console.error('发送验证码失败:', error)
    ElMessage.error('发送验证码失败，请稍后重试')
  }
}

const handleLogin = async () => {
  if (!formRef.value) return
  
  if (!loginForm.agreement) {
    ElMessage.warning('请先同意用户协议和隐私政策')
    return
  }

  try {
    await formRef.value.validate()
    loading.value = true

    // 手机号登录请求
    await userStore.mobileLogin({
      phone: loginForm.phone,
      smsCode: loginForm.smsCode
    })

    // 登录成功后的处理
    const redirect = route.query.redirect as string
    router.push(redirect || '/')
    ElMessage.success('登录成功')
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录失败，请检查手机号和验证码')
  } finally {
    loading.value = false
  }
}

const handleQuickLogin = (type: string) => {
  if (!loginForm.agreement) {
    ElMessage.warning('请先同意用户协议和隐私政策')
    return
  }
  // 处理第三方快捷登录逻辑
  console.log('快捷登录类型:', type)
}

const handleAgreementChange = (value: boolean) => {
  loginForm.agreement = value
}

const showAgreement = () => {
  // 显示用户协议
  console.log('显示用户协议')
}

const showPrivacy = () => {
  // 显示隐私政策
  console.log('显示隐私政策')
}

// 组件卸载时清理定时器
onUnmounted(() => {
  if (timer.value) {
    clearInterval(timer.value)
  }
})
</script>

<style lang="scss" scoped>
.mobile-login-form {
  width: 100%;
  padding: 15px;

  .sms-code-container {
    display: flex;
    gap: 10px;
    align-items: center;

    .el-button {
      width: 120px;
      white-space: nowrap;
    }
  }

  .quick-login {
    margin-top: 20px;

    &-icons {
      display: flex;
      justify-content: center;
      gap: 40px;
      margin-top: 15px;
    }

    &-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      cursor: pointer;

      .icon {
        margin-bottom: 5px;
        font-size: 32px;
        color: var(--el-color-primary);
      }

      span {
        font-size: 12px;
        color: var(--el-text-color-secondary);
      }
    }
  }

  .agreement {
    margin-top: 15px;
    font-size: 12px;
    color: var(--el-text-color-secondary);
    text-align: center;

    .el-link {
      margin: 0 4px;
      font-size: 12px;
    }
  }
}
</style> 